﻿using System;
using System.Collections.Generic;
using System.Linq;
using Biz.EF.Platform;
using Hjt212Parser;
using log4net;


namespace Biz.Buffer
{
    public static class CommonInformation
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(CommonInformation));
        private static int BufferTimespan = 10;

        private static List<TableMapping> _tableMappings;
        /// <summary>
        /// 根据工况采集设备mn获取TableMapping
        /// </summary>
        /// <param name="mn"></param>
        /// <returns></returns>
        public static TableMapping GetTableMappingByGkcjMn(string mn)
        {
            try
            {
                if (_tableMappings == null || MyTimer.IsToTheTime("CommonInformation.GetTableMappingByGkcjMn", BufferTimespan))
                {
                    using (var pCtx = new PlatformContext())
                    {
                        _tableMappings = pCtx.TableMappings.AsNoTracking().ToList();
                    }
                }
                var item = _tableMappings.FirstOrDefault(t => t.mn.Equals(mn));
                return item;
            }
            catch (Exception e)
            {
                Log.Error(e);
            }
            return null;

        }
        public static string GetFieldNameByGkcjMnAndFactor(string mn, string node, string factor)
        {
            try
            {
                using (var pCtx = new PlatformContext())
                {
                    var item = pCtx.TableMappings.FirstOrDefault(t => t.mn.Contains(mn));
                    var detail = item?.TableMappingDetails.FirstOrDefault(t => t.code.Equals(factor) && t.node.Equals(node));
                    return detail?.table_field;
                }
            }
            catch (Exception e)
            {
                Log.Error(e);
            }

            return null;
        }

        //public static string GetFieldNameByGkcjMnAndFactor(string mn, string node, string factor)
        //{
        //    try
        //    {
        //        if (_tableMappings == null || MyTimer.IsToTheTime("CommonInformation.GetFieldNameByGkcjMnAndFactor", BufferTimespan))
        //        {
        //            using (var pCtx = new PlatformContext())
        //            {
        //                _tableMappings = pCtx.TableMappings.AsNoTracking().ToList();
        //            }
        //        }
        //        var item = _tableMappings.FirstOrDefault(t => t.mn.Contains(mn));
        //        var detail = item?.TableMappingDetails.FirstOrDefault(t => t.code.Equals(factor));
        //        return detail?.table_field;
        //    }
        //    catch (Exception e)
        //    {
        //        Log.Error(e);
        //    }

        //    return null;
        //}
        /// <summary>
        /// 根据排口数采mn获取TableMapping
        /// </summary>
        /// <param name="mn"></param>
        /// <returns></returns>
        public static TableMapping GetTableMappingByPkscMn(string mn)
        {
            if (_tableMappings == null || MyTimer.IsToTheTime("CommonInformation.GetTableMappingByPkscMn", BufferTimespan))
            {
                using (var pCtx = new PlatformContext())
                {
                    _tableMappings = pCtx.TableMappings.AsNoTracking().ToList();
                }
            }
            var item = _tableMappings.FirstOrDefault(t => t.port_mn.Equals(mn));
            return item;
        }

        private static List<MonitorFactorCode> _monitorFactorCodes;

        public static MonitorFactorCode GetMonitorFactorCodes(string code)
        {
            if (_monitorFactorCodes == null || MyTimer.IsToTheTime("CommonInformation._monitorFactorCodes", BufferTimespan))
            {
                using (var pCtx = new PlatformContext())
                {
                    _monitorFactorCodes = pCtx.MonitorFactorCodes.AsNoTracking().ToList();
                }
            }
            var item = _monitorFactorCodes.FirstOrDefault(t => t.污染物代码2005.Equals(code));
            if (item == null)
            {
                item= _monitorFactorCodes.FirstOrDefault(t => t.污染物代码.Equals(code));
            }
            return item;
        }


    }


}
